<?php

class FileTest extends MasterTest {

    private $test = true ;

    private $fv ;
    private $fm ;
    private $fc ;

    private $lm ;

    public function __construct() {
        parent::__construct() ;     // We have to make sure that the parent-construct is ran.
        require_once("../Controllers/FileController.php") ;
        require_once("../Models/LoginModel.php") ;
        require_once("../Views/FileView.php") ;
        require_once("../Models/FileModel.php") ;
        $this->fm = new FileModel() ;
        $this->lm = new LoginModel() ;
        $this->fv = new FileView($this->fm) ;
        $this->fc = new FileController($this->fv, $this->fm, $this->lm) ;

        // Set path to test-files
        $this->fm->setPath("Files/Upload/") ;
    }

    public function Tests() {

        if(!$this->FileControllerTests()) {
            $this->test = false ;
        }
        if(!$this->FileViewTests()) {
            $this->test = false ;
        }
        if(!$this->FileModelTests()) {
            $this->test = false ;
        }


        return $this->test ;
    }

    public function FileControllerTests() {

        $retBool = true ;

        // Check first without beeing loged in.

        $this->lm->doLogout() ;

        $notExpOutput = "fileUpload" ;

        if(strpos($this->fc->doControll(), $notExpOutput)) {
          
            array_push($this->errors, array("line"=>__LINE__, "class"=>"FileController", "function"=>"doControll()", "msg"=>"Something went wrong in the ControllerTest")) ;
            $retBool = false ;
        }       

        // And then we log-in
        $this->lm->doLogin("user", "5f4dcc3b5aa765d61d8327deb882cf99") ;
        
        $expOutput = "fileUpload" ; 

        if(!strpos($this->fc->doControll(), $expOutput)) {
          
            array_push($this->errors, array("line"=>__LINE__, "class"=>"FileController", "function"=>"doControll()", "msg"=>"Something went wrong in the ControllerTest")) ;
            $retBool = false ;
        }  

        return $retBool ;

    }

    public function FileViewTests() {

        $retBool = true ;

        // Test the show-files view
        $expOutput = "<a href=\"Files/Upload/test.txt\">test.txt</a><br />\n" ;

        if(!strpos($this->fv->showFileList(array("test.txt")), $expOutput)) { 
            array_push($this->errors, array("line"=>__LINE__, "class"=>"FileView", "function"=>"showFileList()", "msg"=>"Cannot list the files")) ;

            $retBool = false ;
        }

        return $retBool ;

    }

    public function FileModelTests() {
        
        $retBool = true ;

        // Check to see so we get the list with files in the test directory
        $expOutput = array("test.txt") ;

        if($this->fm->getFiles() !== $expOutput) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"FileModel", "function"=>"getFiles()", "msg"=>"Cannot list the files")) ;
            $retBool = false ;
        }  

        $file = array(
                'name' => 'uploadTest.txt',
                'tmp_name' => 'Files/uploadTest.txt',
                'type' => 'text/plain',
                'size' => 32,
                'error' => 0
        ); 

        // And we test to add another file, this don't really work since it don't move the file somewhere...    
        if(!$this->fm->saveFile($file)) {
             array_push($this->errors, array("line"=>__LINE__, "class"=>"FileModel", "function"=>"saveFile()", "msg"=>"Cannot save the files")) ;
             $retBool = false ;
        } 
        
        return $retBool ;

    }

   

}
